// TextForm.java
// A simple label/field this panel
//
import java.text.ParseException;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class CalendarForm extends JPanel implements ActionListener{

  private JTextField[] fields;
  private String[] labels = { "Summary", "Start Time", "End Time", "Location", "Importance", "Description"};
  private char[] mnemonics = { 'F', 'M', 'L', 'A', 'D', 'Z' };
  private int[] widths = { 15, 15, 15, 15, 15, 15};
  private String[] descs = { "Event Summary", "Start Time", "End Time", "Event Location", "Event/priority", "Description" };
  private String[] tips = {};
  private JFrame f;
  private Calendar cal;
  private JTextField summaryField, startTimeField, endTimeField, locationField, descriptionField;
  private JComboBox<String> priorityBox, privacyBox;
  private JLabel lab;
  private JPanel pan;

  // Create a this with the specified labels, tooltips, and sizes.
  private void initForm(){
    JPanel labelPanel = new JPanel(new GridLayout(labels.length + 1, 1));
    JPanel fieldPanel = new JPanel(new GridLayout(labels.length + 1, 1));
    add(labelPanel, BorderLayout.WEST);
    add(fieldPanel, BorderLayout.CENTER);
    // fields = new JTextField[labels.length];

    // Set Summary
    summaryField = new JTextField();
    summaryField.setColumns(30);
    lab = new JLabel("Event Summary", JLabel.RIGHT);
    lab.setLabelFor(summaryField);
    labelPanel.add(lab);
    pan = new JPanel(new FlowLayout(FlowLayout.LEFT));
    pan.add(summaryField);
    fieldPanel.add(pan);

    startTimeField = new JTextField();
    startTimeField.setColumns(30);
    lab = new JLabel("Start Time", JLabel.RIGHT);
    lab.setLabelFor(startTimeField);
    labelPanel.add(lab);
    pan = new JPanel(new FlowLayout(FlowLayout.LEFT));
    pan.add(startTimeField);
    fieldPanel.add(pan);

    endTimeField = new JTextField();
    endTimeField.setColumns(30);
    lab = new JLabel("End Time", JLabel.RIGHT);
    lab.setLabelFor(endTimeField);
    labelPanel.add(lab);
    pan = new JPanel(new FlowLayout(FlowLayout.LEFT));
    pan.add(endTimeField);
    fieldPanel.add(pan);

    locationField = new JTextField();
    locationField.setColumns(30);
    lab = new JLabel("Location", JLabel.RIGHT);
    lab.setLabelFor(locationField);
    labelPanel.add(lab);
    pan = new JPanel(new FlowLayout(FlowLayout.LEFT));
    pan.add(locationField);
    fieldPanel.add(pan);


    privacyBox = new JComboBox<String>();
    privacyBox.setPrototypeDisplayValue("Select a test thingy");
    privacyBox.addItem("PUBLIC");
    privacyBox.addItem("PRIVATE");
    privacyBox.addItem("CONFIDENTIAL");
    lab = new JLabel("Privacy", JLabel.RIGHT);
    lab.setLabelFor(privacyBox);
    labelPanel.add(lab);
    pan = new JPanel(new FlowLayout(FlowLayout.LEFT));
    pan.add(privacyBox);
    fieldPanel.add(pan);

    priorityBox = new JComboBox<String>();
    priorityBox.setPrototypeDisplayValue("Select a test thingy");
    priorityBox.addItem("0");
    priorityBox.addItem("1");
    priorityBox.addItem("2");
    priorityBox.addItem("3");
    priorityBox.addItem("4");
    priorityBox.addItem("5");
    lab = new JLabel("Importance", JLabel.RIGHT);
    lab.setLabelFor(priorityBox);
    labelPanel.add(lab);
    pan = new JPanel(new FlowLayout(FlowLayout.LEFT));
    pan.add(priorityBox);
    fieldPanel.add(pan);

    descriptionField = new JTextField();
    descriptionField.setColumns(30);
    lab = new JLabel("Descript", JLabel.RIGHT);
    lab.setLabelFor(descriptionField);
    labelPanel.add(lab);
    pan = new JPanel(new FlowLayout(FlowLayout.LEFT));
    pan.add(descriptionField);
    fieldPanel.add(pan);

    /*
    for (int i=0; i < labels.length; i+=1) {
      fields[i] = new JTextField();
      if (i < tips.length) fields[i].setToolTipText(tips[i]);
      if (i < widths.length) fields[i].setColumns(widths[i]);

      JLabel lab = new JLabel(labels[i], JLabel.RIGHT);
      lab.setLabelFor(fields[i]);
      if (i < mnemonics.length) lab.setDisplayedMnemonic(mnemonics[i]);

      labelPanel.add(lab);
      JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
      p.add(fields[i]);
      fieldPanel.add(p);
    }
    */



    JButton submit = new JButton("Add New Event");
    JButton clear = new JButton("Clear Form");
    submit.addActionListener(this);
    clear.addActionListener(this);

    f = new JFrame("Team Yukon Calendar GUI");

    // Create our menubar
    JMenuBar menuBar;
    JMenu menu, submenu;
    JMenuItem menuItem;

    //Create the menu bar.
    menuBar = new JMenuBar();

    //Build the first menu.
    menu = new JMenu("File");
    menu.setMnemonic(KeyEvent.VK_A);
    menu.getAccessibleContext().setAccessibleDescription(
            "The only menu in this program that has menu items");
    menuBar.add(menu);

    menuItem = new JMenuItem("Build Calendar", KeyEvent.VK_T);
    menuItem.getAccessibleContext().setAccessibleDescription( "This builds the calendar into an ICS file.");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    menuItem = new JMenuItem("Clear Calendar", KeyEvent.VK_T);
    menuItem.getAccessibleContext().setAccessibleDescription( "Clear the calendar entries.");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    menuItem = new JMenuItem("Display Events", KeyEvent.VK_T);
    menuItem.getAccessibleContext().setAccessibleDescription( "Clear the calendar entries.");
    menuItem.addActionListener(this);
    menu.add(menuItem);
    f.setJMenuBar(menuBar);

    // Set the JFrame Options
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(this, BorderLayout.NORTH);
    JPanel p = new JPanel();
    p.add(submit);
    p.add(clear);


    f.getContentPane().add(p, BorderLayout.SOUTH);
    f.pack();
    f.setVisible(true);
  }

  public CalendarForm(){
    super(new BorderLayout());
    cal = new Calendar(1);
    initForm();
  }

  private void clearTextFields(){
      summaryField.setText("");
      startTimeField.setText("");
      endTimeField.setText("");
      locationField.setText("");
      descriptionField.setText("");
      priorityBox.setSelectedItem("0"); 
      privacyBox.setSelectedItem("PUBLIC");
    
  }

    public void actionPerformed(ActionEvent e) {
      System.out.println("ACTION: "+ e.getActionCommand());
      if("Add New Event".equals(e.getActionCommand())){
          try{
              String summary = summaryField.getText();
              String startTime =  startTimeField.getText();
              String endTime =  endTimeField.getText();
              String location = locationField.getText();
              int importance =  Integer.parseInt((String)priorityBox.getSelectedItem());
              String description = descriptionField.getText();
              String privacy = (String) privacyBox.getSelectedItem();

              System.out.println();
              System.out.println("+----------------------------------------+");
              System.out.println("START CREATE CALENDAR: ");
              System.out.println("Event Summary: " + summaryField.getText());
              System.out.println("Start Time: " + startTimeField.getText());
              System.out.println("End Time: " + endTimeField.getText());
              System.out.println("Location: " + locationField.getText());
              System.out.println("Importance: " + Integer.parseInt((String)priorityBox.getSelectedItem()));
              System.out.println("Description: " + descriptionField.getText());
              System.out.println("Privacy: " + (String) privacyBox.getSelectedItem());
              System.out.println("END CREATE CALENDAR: ");
              System.out.println("+----------------------------------------+");
              System.out.println();

            Event coolEvent = new Event(startTime, endTime, importance, description, summary, location, "PRIVATE");
            System.out.println(coolEvent.buildEvent());
            cal.addEvent(coolEvent);
            JOptionPane.showMessageDialog(null, "Your event was entered.");
          }
          catch(ParseException error){
            System.out.println("Parse error!");
          }
          catch(NumberFormatException error){
            JOptionPane.showMessageDialog(null, "You entered an incorrect importance.");
          }

        }
        else if("Add Event".equals(e.getActionCommand())){
          JOptionPane.showMessageDialog(null, "This feature is not implemented yet.");
        }
        else if("Build Calendar".equals(e.getActionCommand())){
          System.out.println(cal.buildCal());
          cal.buildFile();
          JOptionPane.showMessageDialog(null, "Your calendar is built.");
        }
        else if("Display Events".equals(e.getActionCommand())){
            JOptionPane.showMessageDialog(null, cal.getEvents());
        }
        else if("Clear Form".equals(e.getActionCommand())){
            clearTextFields();
        }
        else if("Clear Calendar".equals(e.getActionCommand())){
            clearCalendar();
            JOptionPane.showMessageDialog(null, "Calendar is cleared.");
        }
    }

  public String getText(int i) {
    return( fields[i].getText() );
  }

  public void clearCalendar(){
    cal =  new Calendar(1);
  }

  public static void main(String[] args) {
    System.out.println("Fun");
    // makeGui();
  }
}
